<template>
  <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :before-close="cancel">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="mini">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="产品" prop="appcode">
            <el-select v-model="form.appcode" @change="_productChange" placeholder="产品" filterable clearable style="width: 100%">
              <el-option
                v-for="(item, index) in productList"
                :key="item + index"
                :label="item.name"
                :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="渠道" prop="channel">
            <el-select v-model="form.channel" placeholder="渠道" filterable clearable style="width: 100%">
              <el-option
                v-for="(item, index) in channelList"
                :key="item + index"
                :label="item.name"
                :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="版本" prop="version">
            <el-input v-model="form.version" placeholder="请输入版本号" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="审核" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio :label="0">否</el-radio>
              <el-radio :label="1">是</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="风控设备" prop="riskDeviceVal">
            <el-input v-model="form.riskDeviceVal" placeholder="请输入风控设备" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20" v-if="form.status == 0">
        <el-col :span="24">
          <el-form-item label="图标隐藏(旧)" prop="iconHidden">
            <el-input v-model="form.iconHidden" placeholder="请输入隐藏城市" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import commonMinxis from './common'
import { auditConfig } from '@/api/configs/appConfig.js'
export default {
  mixins: [commonMinxis],
  props: {
    title: {
      default: '审核配置',
      type: String
    },
    open: {
      default: false,
      type: Boolean
    }
  },
  data () {
    return {
      form: {
        appcode: '',
        channel: '',
        version: '',
        status: 1,
        riskDeviceVal: '[{"osversion":"all","brand":"all"}]',
        iconHidden: '北京市,上海市,武汉市,成都市,深圳市,杭州市,广州市,天津市,福州市,厦门市,重庆市,东莞市,西安市,南京市,珠海市,苏州市,长沙市'
      },
      rules: {
        appcode: [{ required: true, message: '产品不能为空', trigger: 'change' }],
        channel: [{ required: true, message: '渠道不能为空', trigger: 'change' }],
        version: [{ required: true, message: '版本不能为空', trigger: 'change' }],
        riskDeviceVal: [{ required: true, message: '风控设备不能为空', trigger: 'blur' }],
      },
      productList: [],
      channelList: []
    }
  },
  methods: {
    submitForm () {
      let vm = this
      vm.$refs["form"].validate(valid => {
        if (valid) {
          let params = {...vm.form};
          if (params.status == 1) delete params.iconHidden;
          auditConfig(params).then(response => {
            if (response.code === 200) {
              vm.$modal.msgSuccess('操作成功');
              vm._resetForm()
              vm.cancel()
            } else {
              vm.$modal.msgError(response.msg);
            }
          })
        }
      });
    },
    _resetForm () {
      let vm = this;
      vm.form = {
        appcode: '',
        channel: '',
        version: '',
        status: 1,
        riskDeviceVal: '[{"osversion":"all","brand":"all"}]',
        iconHidden: '北京市,上海市,武汉市,成都市,深圳市,杭州市,广州市,天津市,福州市,厦门市,重庆市,东莞市,西安市,南京市,珠海市,苏州市,长沙市'
      };
    },
    cancel () {
      this.$emit('update:open', false)
    }
  }
}
</script>